package com.example.jobback.schedule;

import com.example.jobback.service.recruiter.RecruiterCrossService;
import com.example.jobback.util.CACHEUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.example.jobback.util.RedisConstants.HOT_COMPANY_CITIES_KEY;
import static com.example.jobback.util.RedisConstants.HOT_POSITION_CITIES_KEY;

/**
 * @ClassName: HotCitySyncTask
 * @Description:热门城市设置定时任务
 * @Author: lhb
 * @Date: 2025/7/15
 */
@Component
@Slf4j
public class HotCitySyncTask {

    private final CACHEUtil cacheUtil;

    private final RecruiterCrossService recruiterCrossService;

    public HotCitySyncTask(CACHEUtil cacheUtil, RecruiterCrossService recruiterCrossService) {
        this.cacheUtil = cacheUtil;
        this.recruiterCrossService = recruiterCrossService;
    }

    /**
     * @Description: 每两小时执行一次，设置热门职位城市
     * @return void
     * @Author lhb
     * @CreateTime 2025/7/15 10:31
     */
    @Scheduled(cron = "${cron.hotCities}")
    public void syncHotPositionCities() {
        log.info("开始同步热门岗位城市...");
        List<String> cityList = recruiterCrossService.getHotPositionCities();
        if(cityList.isEmpty()){
            cacheUtil.deleteKeySafely(HOT_POSITION_CITIES_KEY);
            return;
        }
        cacheUtil.deleteKeySafely(HOT_POSITION_CITIES_KEY);
        cacheUtil.setSet(HOT_POSITION_CITIES_KEY, cityList);
    }

    /**
     * @Description: 每两小时执行一次，设置热门企业城市
     * @return void
     * @Author lhb
     * @CreateTime 2025/7/23 15:58
     */
    @Scheduled(cron = "${cron.hotCities}")
    public void syncHotCompanyCities() {
        log.info("开始同步热门企业城市...");
        List<String> cityList = recruiterCrossService.getHotCompanyCities();
        if(cityList.isEmpty()) {
            cacheUtil.deleteKeySafely(HOT_COMPANY_CITIES_KEY);
            return;
        }
        cacheUtil.deleteKeySafely(HOT_COMPANY_CITIES_KEY);
        cacheUtil.setSet(HOT_COMPANY_CITIES_KEY, cityList);
    }
}
